// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace TaskbarConcepts
{
    /// <summary>
    /// Interaction logic for Overlays.xaml
    /// </summary>
    public partial class Overlays : UserControl
    {
        public Overlays()
        {
            InitializeComponent();
            this.Loaded += Overlays_Loaded;
            this.Unloaded += Overlays_Unloaded;
        }

        private void Overlays_Unloaded(object sender, RoutedEventArgs e)
        {
            TaskbarManager.Instance.SetOverlayIcon(null, null);
        }

        private void Overlays_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.iconsList.ItemsSource == null)
            {
                this.iconsList.ItemsSource = Overlays.Icons;
            }
            else
            {
                ShowOrHideOverlayIcon();
            }
        }

        static List<Icon> _icons;
        public static List<Icon> Icons
        {
            get
            {
                if (_icons == null)
                {
                    _icons = new List<Icon>()
                    { 
                        TaskbarConcepts.Resources.questionIcon, 
                        TaskbarConcepts.Resources.noVolume, 
                        TaskbarConcepts.Resources.infoIcon 
                    };
                }
                return _icons;
            }
        }

        private void ShowOverlay_Click(object sender, RoutedEventArgs e)
        {
            ShowOrHideOverlayIcon();
        }

        private void iconsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowOrHideOverlayIcon();
        }

        //The following method is part of the lab. If we are asked to show an 
        //overlay icon, use the TaskbarManager.SetOverlayIcon method to set it.
        //Use the same method with a null icon to reset it, so that no icon is
        //displayed.
        private void ShowOrHideOverlayIcon()
        {
            if (ShowOverlay.IsChecked.Value)
            {
                Icon icon = iconsList.SelectedItem as Icon;
                if (icon != null)
                    TaskbarManager.Instance.SetOverlayIcon(icon, "icon" + iconsList.SelectedIndex.ToString());
            }
            else
            {
                TaskbarManager.Instance.SetOverlayIcon(null, null);
            }
        }
    }

    public class IconToBitmapSourceConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Icon icon = value as Icon;
            if (icon != null)
            {
                BitmapSource bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                    icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                return bitmap;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}